package com.onluy.provider.department.controller;


import java.util.List;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppController;
import com.onluy.frameworkcloud.base.AppService;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.frameworkcloud.tools.utils.StringUtil;
import com.onluy.provider.department.entity.SysDepart;
import com.onluy.provider.department.service.SysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController
@RequestMapping("/depart")
@Api(value="depart", description="/部门管理接口", position=1 ) 
public class SysDepartController extends AppController<SysDepart> {


	@Autowired(required=true)
	private SysDepartService sysDepartService;

	@Override
	public AppService service() {
		return sysDepartService;
	}




	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据departCode获取信息")
	public RspInfo<SysDepart> dotarget(@RequestBody ReqInfo<ReqHead,SysDepart> reqInfo   ) throws Exception {
		log.error("dotarget sysDepart:"+ JSON.toJSONString(reqInfo));
		if(StringUtil.isEmpty(reqInfo.getReqBody().getDepartCode())){
			throw new Exception("departCode不能为空");
		}
		return super.Model(reqInfo.getReqBody());
	}


	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增部门")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,SysDepart> reqInfo ) {
		log.error("dotarget sysDepart:"+ JSON.toJSONString(reqInfo));
		return super.Add(reqInfo.getReqBody());
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="更新部门")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,SysDepart> reqInfo ) {
		log.error("dotarget sysDepart:"+ JSON.toJSONString(reqInfo));

		return super.Update(reqInfo.getReqBody());
	}

	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="删除部门")
	public RspInfo<InParam> doDel( @RequestBody    ReqInfo<ReqHead,SysDepart> reqInfo) throws Exception {
		log.error("dotarget sysDepart:"+ JSON.toJSONString(reqInfo));
		if(reqInfo.getReqBody().getId()==null || reqInfo.getReqBody().getId()<=0){
			throw new Exception("部门ID不能为空");
		}
		return super.Del(reqInfo.getReqBody());
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST})
	@ApiOperation(value="列表", notes="部门列表明细")
	public RspInfo<PageInfo<SysDepart>>  doList(     @RequestBody    ReqInfo<ParamPagination,SysDepart> reqInfo ) {
		log.error("dotarget sysDepart:"+ JSON.toJSONString(reqInfo));
//		if(reqInfo.getReqBody().getDepartId()==null || reqInfo.getReqBody().getDepartId() <0 ) {
//			reqInfo.getReqBody().setDepartId(    Long.parseLong("2")  );
//		}
		return super.List(reqInfo.getReqBody(),reqInfo.getReqHead() );
	}



	@RequestMapping(value={ "/simplelist"},method={RequestMethod.POST})
	@ApiOperation(value="简要列表", notes="部门列表明细")
	public RspInfo<PageInfo<SysDepart>> dosimplelist( @RequestBody    ReqInfo<ParamPagination,SysDepart> reqInfo ) {
		log.error("dotarget sysDepart:"+ JSON.toJSONString(reqInfo));
		return  super.simplelist( reqInfo.getReqBody(),reqInfo.getReqHead() );
	}




	@RequestMapping(value={ "/tree"},method={RequestMethod.POST})
	@ApiOperation(value="部门树", notes="部门树明细信息 增加childCount属性")
	public RspInfo<List<SysDepart>> tree(@RequestBody    ReqInfo<ReqHead,SysDepart> reqInfo  ) {
		List<SysDepart> list =sysDepartService.treelist(reqInfo.getReqBody());
		RspInfo<List<SysDepart>> jsonResult=new RspInfo<List<SysDepart>>();
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}



	@RequestMapping(value={ "/navlist"},method={RequestMethod.POST})
	@ApiOperation(value="部门导航树", notes="部门树简要信息 增加childCount属性")
	public RspInfo<List<SysDepart>> navlist(@RequestBody    ReqInfo<ReqHead,SysDepart> reqInfo  ) {
		List<SysDepart> list=sysDepartService.navlist(reqInfo.getReqBody());
		RspInfo<List<SysDepart>> jsonResult=new RspInfo<List<SysDepart>>();
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

		

}